package com.chapp.lastFmWs;

import java.io.IOException;

import android.content.Context;

import com.chapp.base.Artist;
import com.chapp.base.Event;
import com.chapp.base.Venue;
import com.chapp.lastFmWs.parser.ArtistSearchParser;
import com.chapp.lastFmWs.parser.EventSearchParser;
import com.chapp.lastFmWs.parser.SearchResponse;
import com.chapp.lastFmWs.parser.VenueSearchParser;
import com.chapp.util.Settings;

public class LastFmService {
	private final static String tag = "LastFmService";

	static RestClientLastFm client;

	public static SearchResponse<Artist> searchArtist(Context context,
			String query, int page) {
		client = new RestClientLastFm(context);
		ArtistSearchParser parser = new ArtistSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchArtist(query, page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

	public static SearchResponse<Venue> searchVenue(Context context,
			String query, int page) {
		client = new RestClientLastFm(context);
		VenueSearchParser parser = new VenueSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchVenue(query, page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

	public static SearchResponse<Event> searchLocationEvents(Context context,
			String query, int page) {
		client = new RestClientLastFm(context);
		EventSearchParser parser = new EventSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchLocationEvents(query, page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

	public static SearchResponse<Event> searchLocationEvents(Context context,
			double latitud, double longitud, int page) {
		client = new RestClientLastFm(context);
		EventSearchParser parser = new EventSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchLocationEvents(latitud, longitud,
					Settings.getDistancePref(context), page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

	public static SearchResponse<Event> searchVenueEvents(Context context,
			String query, int page) {
		client = new RestClientLastFm(context);
		EventSearchParser parser = new EventSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchVenueEvents(query, page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

	public static SearchResponse<Event> searchArtistEvents(Context context,
			String query, int page) {
		client = new RestClientLastFm(context);
		EventSearchParser parser = new EventSearchParser();
		String xmlResponse = null;
		try {
			xmlResponse = client.searchArtistEvents(query, page);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return parser.parse(xmlResponse);
	}

}
